package com.ten.jinxiancun.server.impl;

import com.ten.jinxiancun.entity.Menu;
import com.ten.jinxiancun.mapper.menuMapper;
import com.ten.jinxiancun.server.menuServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class menuServerImpl implements menuServer {
@Autowired
menuMapper mm;

    @Override
    public List<Menu> MenuLogFind(Integer id) {


        List<Menu> list = new ArrayList<>();

        list = mm.MenuLog(id);// 根据用户获取用户拥有的菜单
        List<Menu> TreeNodes = new ArrayList<Menu>();
        for (Menu treeNodes : list) {
            if (treeNodes.getPid() == 0 || treeNodes.getPid() == null) {
                TreeNodes.add(findChildren(treeNodes, list));
            }
        }
        System.out.println(TreeNodes);
        return TreeNodes;
    }

    @Override
    public List<Map<String, Object>> getMenuFind(Map<String, Object> map) {
        return mm.getMenu(map);
    }

    @Override
    public Integer countFind(Map<String, Object> map) {
        return mm.count(map);
    }

    public Menu findChildren(Menu treeNode ,List<Menu> treeList) {
        for (Menu item : treeList) {
            if (treeNode.getId()== item.getPid()) {

                if (treeNode.getChildren()==null) {
                    treeNode.setChildren(new  ArrayList<Menu>());
                }
                treeNode.getChildren().add(findChildren(item, treeList));
            }

        }
        return treeNode;

    }


}
